﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class NhomDonViDAO : SqlDAO
    {
        public const string PROCEDURE_GET_LIST_PAGING = "spNhomDonVi_GetListPaging";
        public const string PROCEDURE_INSERT_UPDATE = "spNhomDonVi_InsertUpdate";
        public const string PROCEDURE_GET_BY_ID = "spNhomDonVi_GetById";
        public const string PROCEDURE_GET_ALL = "spNhomDonVi_GetAll";
        public const string PROCEDURE_GET_NHOM_BY_TYPE = "spNhomDonVi_GetByType";
        public const string PROCEDURE_GET_NHOM_BY_TYPE_NEW = "spNhomDonVi_GetByType_New";
        public const string PROCEDURE_GETALL_NHOM_BY_DONVI = "spNhomDonVi_GetByDonViID";
        public const string PROCEDURE_DELETE = "spNhomDonVi_Delete";

        public const string PARAM_NHOM_DON_VI_ID = "@NHOM_DON_VI_ID";
        public const string PARAM_DON_VI_ID = "@DON_VI_ID";
        public const string PARAM_TEN_NHOM_DON_VI = "@TEN_NHOM_DON_VI";
        public const string PARAM_IS_CO_BAN = "@IS_CO_BAN";
        public const string PARAM_GHI_CHU = "@GHI_CHU";
        public const string PARAM_PAGE_INDEX = "@PAGE_INDEX";
        public const string PARAM_PAGE_SIZE = "@PAGE_SIZE";
        public NhomDonViDAO(string connectionString, System.Type type)
            : base(connectionString, type) { ;}

        public DataPage<NhomDonVi> GetListPaging(NhomDonVi model, int pageIndex, int pageSize, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_LIST_PAGING);
                cmd.Parameters.Add(CreateParameter(PARAM_TEN_NHOM_DON_VI, model.TenNhomDonVi));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_CO_BAN, model.IS_CO_BAN));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_INDEX, pageIndex));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_SIZE, pageSize));

                return GetDataPage<NhomDonVi>(cmd, pageIndex, pageSize, mapDictionary);
            }
            catch 
            {
                throw;
            }
        }

        public bool Save(NhomDonVi model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_UPDATE);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, model.NhomDonViId));
                cmd.Parameters.Add(CreateParameter(PARAM_TEN_NHOM_DON_VI, model.TenNhomDonVi));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_CO_BAN, model.IS_CO_BAN));
                cmd.Parameters.Add(CreateParameter(PARAM_GHI_CHU, model.GhiChu));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public NhomDonVi GetById(int id, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, id));

                return GetSingle<NhomDonVi>(cmd, mapDictionary);
            }
            catch 
            {
                throw;
            }
        }

        public List<NhomDonVi> GetAll(Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_ALL);
                //cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, id));

                return GetList<NhomDonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<NhomDonVi> GetNhomDVByType(int intISCoBan, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_NHOM_BY_TYPE);
                cmd.Parameters.Add(CreateParameter(PARAM_IS_CO_BAN, intISCoBan));

                return GetList<NhomDonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<NhomDonVi> GetNhomDVByType_New(int intISCoBan, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_NHOM_BY_TYPE_NEW);
                cmd.Parameters.Add(CreateParameter(PARAM_IS_CO_BAN, intISCoBan));

                return GetList<NhomDonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<NhomDonVi> GetAllNhomByDonViID(int donViID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GETALL_NHOM_BY_DONVI);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, donViID));

                return GetList<NhomDonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, id));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch 
            {
                throw;
            }
        }
    }
}
